﻿/*
创 建 人     ： 陈 锐
创 建 时 间  ： 2012.03.04
*/
Daan = window.Daan || {};
Daan.Map = Daan.Map || {};
Daan.Map.Marker = function () {

};
Daan.Map.CommunityLocalSearch = function (option) {
    var setting = $.extend({
        pageCapacity: 8,
        serviceUrl: "/services/communityhandler/getcommunitysbycity.ashx",
        onSearchComplete: function () { }
    }, option);
    var count = 0;
    var resultInfo = {
        count: 0,
        page: 0,
        index: 0
    };
    var city = "null";
    var getPageComplete = function (result) {

        if (setting.onSearchComplete && typeof setting.onSearchComplete == "function") {
            setting.onSearchComplete({ data: result.data || [], pageInfo: resultInfo });
        }
    };

    var gotoPage = function (index) {
        $.ajax({
            type: "get",
            url: setting.serviceUrl,
            async: true,
            data: { city: city, pageIndex: index, pageSize: setting.pageCapacity },
            cache: true,
            timeout: 10000,
            success: function (result) {
                var results = JSON.parse(result);
                resultInfo.index = index;
                resultInfo.count = results.count || 0;
                resultInfo.page = Math.floor(resultInfo.count / setting.pageCapacity);
                getPageComplete(results);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
            }
        });
    };
    var search = function (scity) {
        city = scity;
        gotoPage(1);
    };

    return { search: search, gotoPage: gotoPage };
};
Daan.Map.CommunitySearch = function (map, option) {
    var setting = $.extend({
        city: "北京市",
        getAllResult: true,
        pageSize: 8,
        forceLocal: true,
        itemGot: function () { }
    }, option);
    var resultInfo = {
        count: 0,
        page: 0,
        index: 0,
        size: setting.pageSize
    };
    var searching = true;
    var started = false;
    var local = new Daan.Map.CommunityLocalSearch({
        pageCapacity: setting.pageSize,
        onSearchComplete: function (results) {
            resultInfo.count = results.pageInfo.count;
            resultInfo.page = results.pageInfo.page;
            resultInfo.index = results.pageInfo.index;
            for (var i = 0, c = results.data.length; i < c; i++) {
                var pos = results.data[i];
                if (typeof setting.itemGot == "function") {
                    setting.itemGot(map, pos);
                }
            }
            if (searching && setting.getAllResult && resultInfo.index < resultInfo.page - 1) {
                local.gotoPage(resultInfo.index + 1);
            }
        }
    });

    function getResultInfo() {
        return resultInfo;
    };

    function getCity() {
        return setting.city;
    }

    function search() {
        if (!searching) {
            searching = true;
            local.gotoPage(resultInfo.index + 1);
        }
        if (!started) {
            started = true;
            local.search(setting.city);
        }
    };

    function stop() {
        searching = false;
    };

    return { getResultInfo: getResultInfo, search: search, stop: stop, getCity: getCity };
};